#include <jni.h>
#include <fcntl.h>
#include <unistd.h>
#include "../gpio/sysfs_gpio.h"
#include "../pwm/sysfs_pwm.h"

#include <android/log.h>
#define TAG "MyJNITag"  // 自定义日志标签
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#include <errno.h>


extern "C" JNIEXPORT void JNICALL
Java_com_example_bus_AndroidJniControl_setLedState(
        JNIEnv* env, jclass clazz, jint gpioPin, jint state) {
    LOGD("Entering setLedState");

    int result1 = SYSFS_GPIO_Direction(gpioPin, 1);
    LOGD("result1: %d", result1);

    int result2 = SYSFS_GPIO_Write(gpioPin, state);
    LOGD("result2: %d", result2);
}

extern "C" JNIEXPORT void JNICALL
Java_com_example_bus_AndroidJniControl_setPwmStart(
        JNIEnv* env, jclass clazz, jint channel, jint period_ns,jint duty_cycle_ns) {
    LOGD("Entering setPwmState");

    //导出
//    pwm_export(channel);

    // 设置周期
    pwm_set_period(channel, period_ns);

    // 设置占空比
    pwm_set_duty_cycle(channel,duty_cycle_ns);
    // 启用 PWM
    pwm_enable(channel, 1);
}

extern "C" JNIEXPORT void JNICALL
Java_com_example_bus_AndroidJniControl_setPwmEnd(
        JNIEnv* env, jclass clazz, jint channel) {
    LOGD("Entering setPwmState");
    pwm_set_duty_cycle(channel,0);
}

